package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.UserTrafficStatistics;
import com.example.mall.dto.UserTrafficStatisticsDto;
import com.example.mall.mapper.UserTrafficStatisticsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserTrafficStatisticsQuery;
import com.example.mall.service.IUserTrafficStatisticsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户访问量统计表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class UserTrafficStatisticsServiceImpl extends ServiceImpl<UserTrafficStatisticsMapper, UserTrafficStatistics> implements IUserTrafficStatisticsService {

    @Autowired
    private UserTrafficStatisticsMapper userTrafficStatisticsMapper;

    @Override
    public PageDTO<UserTrafficStatisticsDto> queryPage(UserTrafficStatisticsQuery userTrafficStatisticsQuery) {
        if (userTrafficStatisticsQuery == null) {
            userTrafficStatisticsQuery = new UserTrafficStatisticsQuery();
        }
        Page<UserTrafficStatistics> page = userTrafficStatisticsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<UserTrafficStatistics> cartPage = lambdaQuery()
                .like(userTrafficStatisticsQuery.getPv()!= null, UserTrafficStatistics::getPv, userTrafficStatisticsQuery.getPv())
                .like(userTrafficStatisticsQuery.getUv()!= null, UserTrafficStatistics::getUv, userTrafficStatisticsQuery.getUv())
                .like(userTrafficStatisticsQuery.getStatId()!= null, UserTrafficStatistics::getStatId, userTrafficStatisticsQuery.getStatId())
                .apply(userTrafficStatisticsQuery.getStatDate() != null,
                        "DATE_FORMAT(stat_date, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        userTrafficStatisticsQuery.getStatDate())
                .orderByDesc(UserTrafficStatistics::getUpdateTime)
                .page(page);
        return PageDTO.of(cartPage, UserTrafficStatisticsDto.class);
    }

    @Override
    public UserTrafficStatistics queryUserTrafficStatisticsById(Long id) {
        AssertUtil.isNotNull(id, "id不能为空");
        return userTrafficStatisticsMapper.selectById(id);
    }

    @Override
    public List<UserTrafficStatistics> queryUserTrafficStatisticsList() {
        return this.list();
    }

    @Override
    public void insert(UserTrafficStatisticsDto userTrafficStatisticsDto) {
        UserTrafficStatistics userTrafficStatistics = new UserTrafficStatistics();
        CopyUtils.copyProperties(userTrafficStatisticsDto, userTrafficStatistics);
        userTrafficStatistics.setCreateTime(new Date());
        userTrafficStatistics.setUpdateTime(new Date());
        userTrafficStatistics.setStatId(CodeGenerateUtils.generateUniqueId());
        userTrafficStatisticsMapper.insert(userTrafficStatistics);
    }

    @Override
    public void update(UserTrafficStatisticsDto userTrafficStatisticsDto) {
        AssertUtil.isNotNull(userTrafficStatisticsDto.getStatId(), "id不能为空");
        UserTrafficStatistics userTrafficStatistics = userTrafficStatisticsMapper.selectById(userTrafficStatisticsDto.getStatId());
        AssertUtil.isNotNull(userTrafficStatistics, "用户访问量统计表不存在");
        CopyUtils.copyProperties(userTrafficStatisticsDto, userTrafficStatistics);
        userTrafficStatistics.setUpdateTime(new Date());
        userTrafficStatisticsMapper.updateById(userTrafficStatistics);
    }

    @Override
    public void delete(Long id) {
        AssertUtil.isNotNull(id, "id不能为空");
        UserTrafficStatistics userTrafficStatistics = userTrafficStatisticsMapper.selectById(id);
        AssertUtil.isNotNull(userTrafficStatistics, "用户访问量统计表不存在");
        userTrafficStatisticsMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        AssertUtil.isNotNull(ids, "id列表不能为空");
        List<UserTrafficStatistics> userTrafficStatistics = userTrafficStatisticsMapper.selectBatchIds(ids);
        AssertUtil.isNotNull(userTrafficStatistics, "用户访问量统计表不存在");
        userTrafficStatisticsMapper.deleteBatchIds(ids);
    }
}
